/**
 * ==========================
 *       == 战斗逻辑 ==
 * ==========================
 */
const SVE = require('./base-tool');
const log = require('./log');
const RSE = require('./base-role');
const MSG = require('../smul_efun/message');
// 基础伤害算法
const Damage = require('../smul_efun/damage');
const damagClass = new Damage();

class COMBAT_D {
    constructor() {}

    /**
     *
     * @param me 玩家
     * @param victim    目标人物
     * @param attack_type   玩家武器类型
     * @param jsonData  武功技能描述
     * @param level 激发的武功有效等级
     */
    do_attack( me, victim, attack_type, jsonData, level, actions) {
        // if (my.start.busy && !victim.start.busy || my.temp.combat_yield) return;

        let my = me;

        if (!my.living ||
            my.qi <= 0 ||
            my.jing <= 0 ||
            my.jingli <= 0 ||
            my.eff_qi + my.qi <= 0 ||
            my.eff_jing + my.jing <= 0
        ) {
            log.err('你的状态不够');
            return;
        }

        /* 不要把力气浪费在已经要昏迷或死亡的人身上 YUJ@SJ 2001-11-16 */
        if (!victim.living && (victim.qi <= 0 || victim.jing <= 0 || victim.jingli <= 0)) return;
        if (victim.eff_qi + victim.qi <= 0 || victim.eff_jing + victim.jing <= 0) return;

        const message = new MSG();
        let you, damage, attack_skill, transfer_str;
        let msg = '';
        let weapon = my.weapon;
        let vweapon;    // object
        // 是否装备上武器
        let isweapon = Object.keys(weapon).length>0
        let result='';

        // 检查武器、更换技能
        if (isweapon) {
            attack_skill = weapon.skill_type;
        } else {
            if (Object.keys(my.prepared).length > 0) {
                // 优先使用准备功夫
                // 缺少：拳掌互备逻辑
                attack_skill = Object.keys(my.prepared)[0];
            } else {
                attack_skill = Object.keys(my.mapped)[0];
            }
        }

        // 敌人武器
        vweapon = victim.weapon;

        // 计算：角色基础伤害
        let temp_damage = Math.floor(my.str / 3) + SVE.random(0, 10);
        my.damage = damagClass.receive_damage("qi", temp_damage, my, victim);

        // 计算：角色基础防御力
        my.temp.defense = Math.floor(my.mapped.force.level / 5);
        // 计算：角色基础攻击力
        my.temp.attack = my.str + my.str_last;
        // 确定角色使用技能
        my.temp.fight = {};
        my.temp.fight[attack_skill] = my.mapped[attack_skill].name;

        // 计算：攻击值
        let ap = RSE.skill_power( my, attack_skill, SKILL_USAGE_ATTACK );
        // let vap = RSE.skill_power( victim, attack_skill, SKILL_USAGE_ATTACK );
        log.damage('攻击-ap', ap);
        // log.damage('vap', vap);

        // 计算躲闪：两角色对比，数值高的随机躲
        let dp = RSE.skill_power( my, "dodge", SKILL_USAGE_DEFENSE );
        // let vdp = RSE.skill_power( victim, "dodge", SKILL_USAGE_DEFENSE );
        dp = SVE.random(Math.floor(dp) / 3, dp);
        // log.damage('躲闪-dp',dp);

        // 计算架招
        let foo =  Object.keys(my.prepared).length>0 ? Object.keys(my.prepared)[0] : "unarmed";
        let pp = RSE.skill_power( my, foo, SKILL_USAGE_PARRY );
        pp = SVE.random(Math.floor(pp) / 3, pp);
        // log.damage('架招-pp', pp)

        // 计算命中，基本为100%

        // 计算伤害
        let damage_rand_min = Math.floor((ap*.5 + level * 4) * .4);
        let damage_rand_max = Math.floor((ap*.5 + level * 4) * .5);
        let damage_bonus = SVE.random(damage_rand_min, damage_rand_max);
        log.damage(damage_bonus);

        if ( me.userp && me.env.damage )
        // msg += ( 'WHT "你对" + victim.name + "造成了"RED + damage + WHT "点攻击伤害(总计"RED + total + WHT "，豁免"RED + abs + WHT ")。\n" ');
        // if ( victim.userp && victim.env/damage )
        // tell_object( victim, WHT "" + me.name + "的攻击对你造成了"RED + damage + WHT "点伤害(总计"RED + total + WHT "，豁免"RED + abs + WHT ")。\n"NOR );

        // 普攻出招
            log.msg(ATTACK_INDEX)
        if (ATTACK_INDEX==actions.length) ATTACK_INDEX=0;
        if (!my.temp.action_flag)
            transfer_str = `<p>${actions[ATTACK_INDEX].action} ！</p>`;
        else
            transfer_str = `<p>紧跟着${actions[ATTACK_INDEX].action} ！</p>`;
        ATTACK_INDEX++;

        // 出招：描述
        let tmpmsg = RSE.adj_msg(transfer_str, weapon, attack_skill);
        msg += RSE.message_vision(tmpmsg, me, victim);

        victim.qi -= damage_bonus;

        if (victim.qi<=0) {
            victim.qi = 0;
            // 战斗结束，重置 ATTACK_INDEX
            RSE.end_fight( victim );
            ATTACK_INDEX = 0;
        }

        /* 看看受害者能否还击. */
        msg += RSE.message_damage(damage_bonus, victim);
        // if ( SVE.random( 1,my["int"] ) < 5 ) /* dex -> int */{
        //     msg += RSE.message_vision( "$N一击不中，大惊失色，不觉露出了破绽！\n", me );
        //     // console.log('1--,', msg)
        //     // this.do_attack( victim, my, vweapon );
        // } else {
        //     //"action":"$N面目僵然，四肢挺直，无弯无折，似「行尸走肉」般，刀臂剑腿，横斩直刺$n的$l",
        //     msg += RSE.message_vision( "$N一见$n攻击失误，顿时趁机发动攻击！\n", my, victim );
        //     // console.log('2--,', msg)
        //     // this.do_attack( victim, my, vweapon );
        // }
        return {
            msg:msg,
            living:{
                me_qi:me.qi,
                target_qi:victim.qi
            }
        };
    }
}

module.exports = new COMBAT_D();